<template>
	<view>
		<!-- <text class="positionText">{{locationInfo.province}}</text>
		<text class="positionText">{{locationInfo.city}}</text>
		<text class="positionText">{{locationInfo.district}}</text>
		<text class="positionText">{{locationInfo.positionInfo}}</text> -->
	</view>
</template>

<script>
	/**
	 * 引入腾讯地图微信小程序SDK
	 */
	import QQMapWX from '@/utills/map/qqmap-wx-jssdk.js'
	export default {
		name: "location",
		mounted() {
			// 进入小程序调用授权接口，获取用户当前定位
			this.getLocation()
		},
		data() {
			return {
				locationInfo: {
					province: '',
					city: '',
					district: '',
					positionInfo: '',
					locationLatitude: '',//经度
					locationLongitude: '',//维度
				}, 
			};
		},
		methods: {
			/**
			 * 向父组件传递数据
			 * */
			toParent() {
				this.$emit('watchPosition', {
					data: this.locationInfo
				})
			},
			/**
			 * 获取用户当前定位
			 */
			getLocation() {
				let that = this
				// 向用户发起授权请求，弹框提示
				uni.authorize({
					// 获取用户定位信息
					scope: "scope.userLocation",
					// 用户同意授权执行
					success() {
						// 引入腾讯地图API，实例化核心类
						let qqMapSdk = new QQMapWX({
							// 填入你上面申请的应用key值
							key: 'MIKBZ-QDYC2-5QEU4-CHE3O-ZDNP2-IVBSN'
						})
						// 获取位置信息
						uni.getLocation({
							type: 'wgs84',
							success(result) {
								that.locationInfo.locationLatitude = result.latitude
								that.locationInfo.locationLongitude = result.longitude
								// 逆地址解析方法
								qqMapSdk.reverseGeocoder({
									location: {
										latitude: result.latitude,
										longitude: result.longitude,
									},
									success(res) {
										// console.log(res);
										that.locationInfo.province = res.result.ad_info.province
										that.locationInfo.city = res.result.ad_info.city
										that.locationInfo.district = res.result.ad_info.district
										that.locationInfo.positionInfo = res.result
											.formatted_addresses.recommend
										that.toParent()
									},
									fail(err) {
										console.log('逆地址解析失败');
									}
								})
							}
						})
					},
					fail(err) {
						uni.showToast({
							icon: 'none',
							title: '注意：需要授权获取定位，否则部分功能将无法使用',
							duration: 2000
						})
					}
				})
			},
		}
	}
</script>

<style scoped>

</style>
